package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.DischargeBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.DischargeMapper;
import com.java2207.group1.pojo.Discharge;
import com.java2207.group1.service.DischargeService;
import com.java2207.group1.vo.discharge.BarVo;
import com.java2207.group1.vo.discharge.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 15319 2022/7/25 17:45
 */
@Service("dischargeService")
public class DischargeServiceImpl implements DischargeService {
    @Autowired
    private DischargeMapper dischargeMapper;


    @Override
    public Discharge dischargeById(Discharge discharge) {
        return dischargeMapper.dischargeById(discharge);
    }

    @Override
    public LayuiPageVo<Discharge> dischargePage(Map<String, Object> params) {
        int page = Integer.parseInt(params.get("page") + "");
        int limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page,limit);

        List<Discharge> dischargePage = dischargeMapper.dischargePage(params);
        PageInfo<Discharge> dischargePageInfo = new PageInfo<>(dischargePage);

        LayuiPageVo<Discharge> layuiPageVo = new LayuiPageVo<>();
        layuiPageVo.setCode(0);
        layuiPageVo.setMsg("出院列表");
        layuiPageVo.setCount(dischargePageInfo.getTotal());
        layuiPageVo.setData(dischargePageInfo.getList());

        return layuiPageVo;
    }

    @Override
    public int dischargeUpdate(Discharge discharge) {
        return dischargeMapper.dischargeUpdate(discharge);
    }

    @Override
    public int dischargeDelete(Integer id) {
        return dischargeMapper.dischargeDelete(id);
    }

    @Override
    public int dischargeAdd(Discharge discharge) {
        return dischargeMapper.dischargeAdd(discharge);
    }

    @Override
    public int dischargeDeleteByIds(String ids) {
        String[] id = ids.split(",");
        return dischargeMapper.dischargeDeleteByIds(id);
    }

    @Override
    public List<BarVo> setBarDate() {
        return dischargeMapper.setBarDate();
    }

    @Override
    public List<PieVo> setPieDate() {
        return dischargeMapper.setPieDate();
    }

    @Override
    public List<DischargeBo> dischargeExcelPage(Map<String, Object> params) {
        List<Discharge> dischargePage = dischargeMapper.dischargePage(params);
        List<DischargeBo> collect = dischargePage.stream().map(discharge -> {
            DischargeBo dischargeBo = new DischargeBo();
            dischargeBo.setId(discharge.getId());
            dischargeBo.setName(discharge.getName());
            dischargeBo.setAge(discharge.getAge());
            dischargeBo.setSex(discharge.getSex());
            dischargeBo.setGuardian(discharge.getGuardian());
            dischargeBo.setDischargeDate(discharge.getDischargeDate());
            return dischargeBo;
        }).collect(Collectors.toList());

        return collect;

    }


}

